//
//  NGbbsTableViewCell.swift
//  NingguoForum
//
//  Created by zhengsw on 2025/7/17.
//  Copyright © 2025 zhengsw. All rights reserved.
//

import UIKit

class NGbbsTableViewCell: UITableViewCell {
    var collectionView: UICollectionView!
    var model: ResponseCircleClassModel? = nil
    // 声明回调属性
    var onCollectionViewItemTapped: ((IndexPath) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        let itemWidth = (KSCREEN_WIDTH-20)/2.0
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: itemWidth, height: 60)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        collectionView.layer.masksToBounds = true
        collectionView.layer.cornerRadius = 10
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self;
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(NGbbsTableCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        contentView.addSubview(collectionView)
        
        collectionView.isUserInteractionEnabled = true
        collectionView.allowsSelection = true  // 确保允许选择
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(self).offset(10);
            make.right.equalTo(self).offset(-10);
            make.top.equalToSuperview().offset(10);
            make.bottom.equalTo(self).offset(-10);
        }
        // 添加调试信息
            print("CollectionView 设置:")
            print("- isUserInteractionEnabled: \(collectionView.isUserInteractionEnabled)")
            print("- allowsSelection: \(collectionView.allowsSelection)")
            print("- delegate: \(collectionView.delegate != nil ? "已设置" : "未设置")")
            print("- dataSource: \(collectionView.dataSource != nil ? "已设置" : "未设置")")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setModel(model:ResponseCircleClassModel){
        self.model = model
        collectionView.reloadData()
    }

}


extension NGbbsTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate  {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (self.model?.circle_list?.count)!
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NGbbsTableCollectionViewCell;
        let model = self.model?.circle_list?[indexPath.row]
        cell.setModel(model: model!)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("CollectionView 被点击: \(indexPath)")
        
        // 获取对应的 model
        if let model = self.model?.circle_list?[indexPath.row] {
            print("点击的项目: \(model)")
            // 在这里处理你的业务逻辑
            // 调用回调，将点击事件传递给外部
            onCollectionViewItemTapped?(indexPath)
        }
    }
    
}
